package chapter4_hash;

import java.util.HashMap;

/**
 * @Author yuh
 * @Date Created in 上午7:12 2018/4/10
 * @Description 没有顺序而且要降低时间复杂度的情况要考虑使用table 有序的情况要考虑使用tree
 */
public class TwoSumII {

    static int twoSum(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
        HashMap<Integer, Integer> map = new HashMap<>();
        int all = 0;
        for (int i = 0; i < nums1.length; i++) {
            for (int j = 0; j < nums2.length; j++) {
                int tmp = nums1[i] + nums2[j];
                if (map.get(tmp) != null) {
                    map.put(tmp, map.get(tmp) + 1);
                } else {
                    map.put(tmp, 1);
                }
            }
        }


        for (int i = 0; i < nums3.length; i++) {
            for (int j = 0; j < nums4.length; j++) {
                int tmp = nums3[i]+nums4[j];
                if(map.get(0-tmp) != null){
                    all += map.get(0-tmp);
                }
            }
        }

        return all;
    }

    public static void main(String[] args) {
        int[] nums1 = {1,2,3};
        int[] nums2 = {1,2,3};
        int[] nums3 = {-1,-2,-3};
        int[] nums4 = {-1,-2,-3};
        System.out.println(twoSum(nums1,nums2,nums3,nums4));
    }













}
